package view.dialog;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;

import lib.MyUtils;
import view.Home;

public class InvoiceDialog extends JDialog {

	private static final long serialVersionUID = 1L;
	private JButton btn_invdlg_cancel;
	private JButton btn_invdlg_savechange;
	private JComboBox<String> cbb_invdlg_productID;
	private JLabel lbl_invdlg_productID;
	private JLabel lbl_invdlg_productName;
	private JLabel lbl_invdlg_quantity;
	private JPanel pn_emdlg_changeinfor;
	private JTextField txt_invdlg_productName;
	private JTextField txt_invdlg_quantity;

	private static InvoiceDialog instance;


	
	public static InvoiceDialog getInstance() {
		if (instance == null) {
			return instance = new InvoiceDialog(Home.getInstance(),true);
		}
		return instance;
	}
	private InvoiceDialog(JFrame jFrame, boolean modal) {
		
		super(jFrame, modal);
		initComponents();
		btn_invdlg_cancel.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				MyUtils.doDispose(getInstance());
			}
		});
	}

	private void initComponents() {

		pn_emdlg_changeinfor = new JPanel();
		lbl_invdlg_productID = new JLabel();
		lbl_invdlg_productName = new JLabel();
		lbl_invdlg_quantity = new JLabel();
		txt_invdlg_productName = new JTextField();
		txt_invdlg_quantity = new JTextField();
		btn_invdlg_savechange = new JButton();
		btn_invdlg_cancel = new JButton();
		cbb_invdlg_productID = new JComboBox<String>();

		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

		lbl_invdlg_productID.setFont(new java.awt.Font("Calibri", 0, 12));
		lbl_invdlg_productID.setText("Product ID:");

		lbl_invdlg_productName.setFont(new java.awt.Font("Calibri", 0, 12));
		lbl_invdlg_productName.setText("Product name:");

		lbl_invdlg_quantity.setFont(new java.awt.Font("Calibri", 0, 12));
		lbl_invdlg_quantity.setText("Quantity:");

		btn_invdlg_savechange.setIcon(new ImageIcon("icon/save.png"));
		btn_invdlg_savechange.setText("Save change");

		btn_invdlg_cancel.setIcon(new ImageIcon("icon/cancle.png"));
		btn_invdlg_cancel.setText("Cancel");

		cbb_invdlg_productID.setModel(new DefaultComboBoxModel<String>(
				new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

		GroupLayout pn_emdlg_changeinforLayout = new GroupLayout(
				pn_emdlg_changeinfor);
		pn_emdlg_changeinfor.setLayout(pn_emdlg_changeinforLayout);
		pn_emdlg_changeinforLayout
				.setHorizontalGroup(pn_emdlg_changeinforLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_emdlg_changeinforLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pn_emdlg_changeinforLayout
														.createParallelGroup(
																GroupLayout.Alignment.LEADING)
														.addGroup(
																pn_emdlg_changeinforLayout
																		.createSequentialGroup()
																		.addGroup(
																				pn_emdlg_changeinforLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING)
																						.addComponent(
																								lbl_invdlg_productID)
																						.addComponent(
																								lbl_invdlg_productName))
																		.addGap(16,
																				16,
																				16)
																		.addGroup(
																				pn_emdlg_changeinforLayout
																						.createParallelGroup(
																								GroupLayout.Alignment.LEADING)
																						.addComponent(
																								txt_invdlg_productName)
																						.addComponent(
																								cbb_invdlg_productID,
																								0,
																								GroupLayout.DEFAULT_SIZE,
																								Short.MAX_VALUE)))
														.addGroup(
																pn_emdlg_changeinforLayout
																		.createSequentialGroup()
																		.addComponent(
																				lbl_invdlg_quantity)
																		.addPreferredGap(
																				LayoutStyle.ComponentPlacement.RELATED,
																				GroupLayout.DEFAULT_SIZE,
																				Short.MAX_VALUE)
																		.addComponent(
																				txt_invdlg_quantity,
																				GroupLayout.PREFERRED_SIZE,
																				180,
																				GroupLayout.PREFERRED_SIZE)))
										.addContainerGap())
						.addGroup(
								pn_emdlg_changeinforLayout
										.createSequentialGroup()
										.addGap(30, 30, 30)
										.addComponent(btn_invdlg_savechange,
												GroupLayout.PREFERRED_SIZE,
												120, GroupLayout.PREFERRED_SIZE)
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addComponent(btn_invdlg_cancel,
												GroupLayout.PREFERRED_SIZE,
												120, GroupLayout.PREFERRED_SIZE)
										.addContainerGap(17, Short.MAX_VALUE)));
		pn_emdlg_changeinforLayout
				.setVerticalGroup(pn_emdlg_changeinforLayout
						.createParallelGroup(GroupLayout.Alignment.LEADING)
						.addGroup(
								pn_emdlg_changeinforLayout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												pn_emdlg_changeinforLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_invdlg_productID)
														.addComponent(
																cbb_invdlg_productID,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_emdlg_changeinforLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																lbl_invdlg_productName)
														.addComponent(
																txt_invdlg_productName,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE))
										.addPreferredGap(
												LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												pn_emdlg_changeinforLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																txt_invdlg_quantity,
																GroupLayout.PREFERRED_SIZE,
																GroupLayout.DEFAULT_SIZE,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																lbl_invdlg_quantity))
										.addGap(18, 18, 18)
										.addGroup(
												pn_emdlg_changeinforLayout
														.createParallelGroup(
																GroupLayout.Alignment.BASELINE)
														.addComponent(
																btn_invdlg_savechange,
																GroupLayout.PREFERRED_SIZE,
																30,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																btn_invdlg_cancel,
																GroupLayout.PREFERRED_SIZE,
																30,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		GroupLayout layout = new GroupLayout(getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(pn_emdlg_changeinfor,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addContainerGap()));
		layout.setVerticalGroup(layout.createParallelGroup(
				GroupLayout.Alignment.LEADING).addGroup(
				layout.createSequentialGroup()
						.addContainerGap()
						.addComponent(pn_emdlg_changeinfor,
								GroupLayout.DEFAULT_SIZE,
								GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addContainerGap()));

		pack();
	}

	public static void main(String args[]) {
		try {
			for (UIManager.LookAndFeelInfo info : UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(InvoiceDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(InvoiceDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(InvoiceDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(InvoiceDialog.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}
		// </editor-fold>

		/* Create and display the dialog */
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				InvoiceDialog dialog = new InvoiceDialog(new JFrame(), true);
				dialog.addWindowListener(new java.awt.event.WindowAdapter() {
					@Override
					public void windowClosing(java.awt.event.WindowEvent e) {
						System.exit(0);
					}
				});
				dialog.setVisible(true);
			}
		});
	}

}
